package com.imooc.business.core.executor.up;

import com.imooc.business.context.DataContext;
import com.imooc.module.collect.request.ParamsRequest;
import com.imooc.portal.mq.entity.EventProcessMessage;
import com.imooc.portal.mq.RocketMqUpDimensionProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;

import java.util.UUID;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
@Slf4j
public class RocketMqEventProcess implements EventProcess{
    @Override
    public void process(DataContext context) {
        log.info("维度升级开始...");
        RocketMqUpDimensionProducer.sendMsg(buildMessage(context), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
            }
            @Override
            public void onException(Throwable e) {
                //可以把消息保存本地表，之后可以定时器重试，如果超过一定的次数发送失败，进行预警
                log.error("维度升级消息发送失败, e=", e);
            }
        });
    }

    /**
     * 构建消息实体
     * @param context
     * @return
     */
    private EventProcessMessage buildMessage(DataContext context) {
        EventProcessMessage message = new EventProcessMessage();
        message.setUuid(UUID.randomUUID().toString());
        ParamsRequest request = context.getRequest();
        message.setBusinessId(request.getBusinessId());
        ParamsRequest.DimensionParameter dimension = new ParamsRequest.DimensionParameter();
        dimension.setUserIds(request.getDimensionParameter().getUserIds());
        dimension.setGroupId(request.getDimensionParameter().getGroupId());
        dimension.setSchoolId(request.getDimensionParameter().getSchoolId());
        dimension.setDimension(request.getDimensionParameter().getDimension());
        message.setDimension(dimension);
        message.setPeriodType(request.getPeriodType());
        message.setRequestDate(request.getRequestDate());
        message.setStartTime(request.getStartTime());
        message.setEndTime(request.getEndTime());
        return message;
    }
}
